﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using Kugar.Core.ExtMethod;

namespace Kugar.Tools.SQLHelper.SQLGenerator
{
    public static class T4SQLHelper
    {
        public static string FirstLetterLower(string name)
        {
            return name[0].ToString().ToLower() + name.Substring(1);
        }

        public static string FirstLetterUpper(string name)
        {
            return name[0].ToString().ToUpper() + name.Substring(1);
        }

        public static string GetModuleName(string tableName)
        {
            return tableName.Substring(x => x == '_');
        }

        public static string GetColumnKeyNameParameterName(DbTableStructInfo table)
        {
            return FirstLetterLower(table.Columns.FirstOrDefault(x => x.IsPrimaryKey)?.Name);
        }

        public static string ConvertColumnTypeToNetType(string  valueType,bool isNull)
        {
            valueType = valueType.Trim().ToLower();

            switch (valueType)
            {
                case "bigint":
                {
                    return "long" + (isNull ? "?" : "");
                }
                case "image":
                case "binary":
                case "blob":
                {
                    return "byte[]";
                }
                case "bit":
                {
                    return "bool" + (isNull ? "?" : "");
                }
                case "char":
                {
                    return "string";
                }
                case "date":
                case "datetime":
                case "datetime2":
                case "datetimeoffset":
                case "time":
                case "timestamp":
                {
                    return "DateTime" + (isNull ? "?" : "");
                }
                case "decimal":
                case "money":
                case "smallmoney":
                {
                    return "decimal" + (isNull ? "?" : "");
                }
                case "float":
                {
                    return "float" + (isNull ? "?" : "");
                }
                case "double":
                {
                    return "double" + (isNull ? "?" : "");
                    }
                case "int":
                {
                    return "int" + (isNull ? "?" : "");
                }
                case "nchar":
                case "ntext":
                case "nvarchar":
                case "text":
                case "varchar":
                case "sysname":
                case "longtext":
                {
                    return "string";
                }
                case "tinyint":
                case "smallint":
                {
                    return "short" + (isNull ? "?" : "");
                }
                case "integer":
                {
                    return "long" + (isNull ? "?" : ""); ;
                }
            }

            throw new NotImplementedException("未支持的类型");
        }

        public static Type ConvertColumnTypeToNetType_Type(string valueType, bool isNull)
        {
            valueType = valueType.Trim().ToLower();

            switch (valueType)
            {
                case "bigint":
                {
                    return isNull?typeof(long?):typeof(long);
                }
                case "image":
                case "binary":
                {
                    return typeof(byte[]);
                }
                case "bit":
                {
                    return (isNull ? typeof(bool?) : typeof(bool));
                }
                case "char":
                {
                    return typeof(char);
                }
                case "date":
                case "datetime":
                case "datetime2":
                case "datetimeoffset":
                case "time":
                case "timestamp":
                {
                    return  (isNull ? typeof(DateTime?) : typeof(DateTime));
                }
                case "decimal":
                case "money":
                case "smallmoney":
                {
                    return  (isNull ?typeof(decimal?) : typeof(decimal));
                }
                case "float":
                {
                    return  (isNull ? typeof(float?) : typeof(float));
                }
                case "int":
                {
                    return  (isNull ? typeof(int?) : typeof(int));
                }
                case "nchar":
                case "ntext":
                case "nvarchar":
                case "text":
                case "varchar":
                case "sysname":
                {
                    return typeof(string);
                }
                case "tinyint":
                case "smallint":
                {
                    return  (isNull ? typeof(short?) : typeof(short));
                }
            }

            throw new NotImplementedException("未支持的类型");
        }

        public static string GetFieldCommentTitle(string title)
        {
            var regx=new Regex("(\\w+)\\b");

            var g = regx.Match(title);

            if (g.Success && g.Groups.Count>0)
            {
                return g.Groups[0].Value;
            }
            else
            {
                return title;
            }
        }
    }
}